package cn.tedu.csmall.business.service.impl;

import cn.tedu.csmall.business.service.IBusinessService;
import cn.tedu.csmall.commons.exception.CoolSharkServiceException;
import cn.tedu.csmall.commons.pojo.order.dto.OrderAddDTO;
import cn.tedu.csmall.commons.restful.ResponseCode;
import cn.tedu.csmall.order.service.IOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class BusinessServiceImpl implements IBusinessService {
    @DubboReference
    private IOrderService dubboOrderService;

    //激活Seata分布式事务管理 并开始运行的标记注解
    @GlobalTransactional
    @Override
    public void buy() {
        //模拟一个下单过程
        OrderAddDTO orderAddDTO = new OrderAddDTO()
                .setCommodityCode("PC100")
                .setUserId("UU100")
                .setCount(5)
                .setMoney(888);
        log.info("要新增的订单信息为{}",orderAddDTO);

        //远程调用新增订单的方法
        dubboOrderService.orderAdd(orderAddDTO);

        //编写代码随机抛出异常
        if(Math.random()>0.5){
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"发送随机异常");
        }

    }
}
